package com.smart.translator.data.api;

import android.content.Context;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import java.util.concurrent.TimeUnit;

public class RetrofitClient {
    private static final String BASE_URL = "https://translation-api.example.com/"; // 替换为实际的API地址
    private static Retrofit retrofit = null;
    private static OkHttpClient okHttpClient = null;

    public static void init(Context context) {
        if (okHttpClient == null) {
            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

            okHttpClient = new OkHttpClient.Builder()
                    .addInterceptor(loggingInterceptor)
                    .connectTimeout(30, TimeUnit.SECONDS)
                    .readTimeout(30, TimeUnit.SECONDS)
                    .writeTimeout(30, TimeUnit.SECONDS)
                    .build();
        }

        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .client(okHttpClient)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
    }

    public static <T> T createService(Class<T> serviceClass) {
        if (retrofit == null) {
            throw new IllegalStateException("RetrofitClient not initialized. Call init() first.");
        }
        return retrofit.create(serviceClass);
    }

    public static void reset() {
        retrofit = null;
        okHttpClient = null;
    }

    public static Retrofit getInstance() {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                .baseUrl("https://api.example.com/") // TODO: 替换为你的实际baseUrl
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        }
        return retrofit;
    }
} 